<template>
  <div style="margin-top: 50px">
    <el-row>
      <el-col :xs="{span: 22,offset:1}" :sm="{span: 18,offset:3}" :md="{span: 18,offset:3}" :lg="{span: 18,offset:3}" :xl="{span: 8,offset:8}" align="center">
        <h1>注册</h1>
      </el-col>
    </el-row>
    <el-row>
      <el-col :xs="{span: 22,offset:1}" :sm="{span: 18,offset:3}" :md="{span: 18,offset:3}" :lg="{span: 18,offset:3}" :xl="{span: 8,offset:8}" >
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
          <el-divider>基本信息</el-divider>
          <el-form-item prop="username">
            <el-input type="text" placeholder="请输入账号" v-model="ruleForm.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="pass">
            <el-input type="password" placeholder="请输入密码" v-model="ruleForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="checkPass">
            <el-input type="password" placeholder="请输入确认密码" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="nickname">
            <el-input type="text" placeholder="请输入昵称" v-model="ruleForm.nickname" autocomplete="off"></el-input>
          </el-form-item>
          <br>
          <el-divider>个人信息</el-divider>
          <el-form-item prop="truename">
            <el-input type="text" placeholder="请输入真实姓名" v-model="ruleForm.truename" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="cardcode">
            <el-input type="text" placeholder="请输入身份证" v-model="ruleForm.cardcode" autocomplete="off"></el-input>
          </el-form-item>
          <br>
          <el-divider>工作信息</el-divider>
          <el-form-item prop="company">
            <el-input type="text" placeholder="请输入所在单位" v-model="ruleForm.company" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="工种" prop="profession_id">
            <el-select v-model="ruleForm.profession_id" placeholder="请选择">
              <el-option
                v-for="item in professions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item align="center">
            <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
            <br>已有账号，点击<el-link type="primary" @click="() => {this.$router.push({path: '/'})}">登录</el-link>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { register } from '../../api/auth'
import { fucCheckTEL } from '../../utils/validate'
import { md5 } from 'md5js'
import { getDictionary } from '../../api/home'

export default {
  name: 'Register',
  data () {
    var checkUsername = (rule, value, callback) => {
      if (fucCheckTEL(value)) {
        callback()
      } else {
        callback(new Error('仅支持手机号作为用户名！'))
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      }
      if (value.length < 6) {
        callback(new Error('密码长度必须大于6位'))
      }
      if (this.ruleForm.checkPass !== '') {
        this.$refs.ruleForm.validateField('checkPass')
      }
      callback()
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    var ValidateCard = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入身份证号码'))
      } else if (value.length < 18) {
        callback(new Error('身份证号码错误'))
      } else {
        callback()
      }
    }
    var ValidateName = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入真实姓名'))
      } else if (this.isChinese(value)) {
        callback(new Error('真实姓名请勿乱填'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        password: '',
        username: '',
        checkPass: '',
        cardcode: ''
      },
      professions: [],
      rules: {
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        username: [
          { validator: checkUsername, trigger: 'blur' }
        ],
        cardcode: [
          { validator: ValidateCard, trigger: 'blur' }
        ],
        truename: [
          { validator: ValidateName, trigger: 'blur' }
        ]
      },
      uuid: ''
    }
  },
  methods: {
    isChinese (temp) {
      var re = /.*[\u4e00-\u9fa5]+.*$/
      if (re.test(temp)) return false
      return true
    },
    getDictionary () {
      const code = 'profession'
      getDictionary(code).then(res => {
        this.professions = res.data
      })
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          register({
            username: this.ruleForm.username,
            password: md5(this.ruleForm.password, 32),
            nickname: this.ruleForm.nickname,
            cardcode: this.ruleForm.cardcode,
            company: this.ruleForm.company,
            truename: this.ruleForm.truename
          }).then((res) => {
            console.log(res)
            if (res.code === 1) {
              this.$router.push({
                name: 'regSuccess',
                params: {
                  username: this.ruleForm.username,
                  password: this.ruleForm.password,
                  id: res.data.id
                }
              })
            }
          }).catch((err) => {
            this.$message({
              message: '注册失败——' + err,
              type: 'warning'
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  mounted () {
    this.getDictionary()
  }
}
</script>

<style scoped>

</style>
